import requests

def get_joke_by_keyword(keyword):

    url = f"https://v2.jokeapi.dev/joke/Any?contains={keyword}"

    response = requests.get(url)
    if response.status_code == 200:

        data = response.json()

        if not data['error']:

            if data['type'] == 'single':

                return data['joke']

            elif data['type'] == 'twopart':

                return f"{data['setup']} {data['delivery']}"

    return "没有找到相关的笑话。"


if __name__ == "__main__":
    user_keyword = input("请输入一个关键词:")

    joke = get_joke_by_keyword(user_keyword)

    print("\n找到的笑话是:\n")

    print(joke)

    # 输入的是英文才可以